﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto.WCSTaskDto;
using Zocono.WCS.Infrastructure.ApllicationDto;

namespace Zocono.WCS.Domain.RouteDomain
{
    public class RouteInfoService : IRouteInfoService
    {
        private readonly ILogUtil _LogUtil;
        private readonly IRedisHelper _IRedisHelper;
        private readonly ISnowflake _Snowflake;
        private readonly IRouteInfoRepository _RouteInfoRepository;
        public RouteInfoService(ILogUtil logUtil
            , IRedisHelper iRedisHelper
            , ISnowflake snowflake
            , IRouteInfoRepository routeInfoRepository)
        {
            _LogUtil = logUtil;
            _IRedisHelper = iRedisHelper;
            _Snowflake = snowflake;
            _RouteInfoRepository = routeInfoRepository;
        }

        public async Task<bool> CreateRouteInfoAsync(RouteInfo routeInfo)
        {
            routeInfo.ID = _Snowflake.GetId();
            bool isOk = await _RouteInfoRepository.InsertAsync(routeInfo);

            return isOk;
        }

        public async Task<bool> DeleteRouteInfoAsync(List<RouteInfo> routeInfoList)
        {
            try
            {
                _RouteInfoRepository.AsTenant().BeginTran();

                foreach (var item in routeInfoList)
                {
                    await _RouteInfoRepository.DeleteByIdAsync(item.ID);
                    //级联删除
                    var detailList = await _RouteInfoRepository.AsSugarClient().Queryable<RouteDetailInfo>()
                        .Where(it => it.RouteID == item.ID)
                        .ToListAsync();
                    foreach (var detail in detailList)
                    {
                        await _RouteInfoRepository.AsSugarClient().Deleteable<RouteDetailInfo>()
                            .Where(it => it.ID == detail.ID)
                            .ExecuteCommandAsync();
                    }
                }

                _RouteInfoRepository.AsTenant().CommitTran();

                return true;
            }
            catch (Exception ex)
            {
                _RouteInfoRepository.AsTenant().RollbackTran();

                throw new Exception(ex.Message);
            }
        }
        /// <summary>
        /// 通过ID删除数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<bool> DeleteRouteInfoByIdAsync(long id)
        {
            var res = await _RouteInfoRepository.DeleteByIdAsync(id);
            return res;
        }

        public async Task<PageDto<RouteInfo>> GetPagedRouteInfoAsync(QueryRouteInfoDto queryRouteInto)
        {
            var exp = Expressionable.Create<RouteInfo>();
            exp.AndIF(!string.IsNullOrEmpty(queryRouteInto.FromStation), it => it.FromStation.Contains(queryRouteInto.FromStation));
            exp.AndIF(!string.IsNullOrEmpty(queryRouteInto.ToStation), it => it.ToStation.Contains(queryRouteInto.ToStation));

            PageDto<RouteInfo> pagedData = new PageDto<RouteInfo>();
            RefAsync<int> totalCount = 0;

            var result = await _RouteInfoRepository.AsSugarClient()
                .Queryable<RouteInfo>()
                .Where(exp.ToExpression())
                .ToPageListAsync(queryRouteInto.PageIndex, queryRouteInto.PageSize, totalCount);

            pagedData.Result = result;
            pagedData.Total = totalCount;

            return pagedData;
        }
        /// <summary>
        /// 通过ID获取数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<RouteInfo> GetRouteInfoByIdAsync(long id)
        {
            var res = await _RouteInfoRepository.GetByIdAsync(id);
            return res;
        }

        public async Task<List<RouteInfo>> GetRouteInfoListAsync()
        {
            var list = await _RouteInfoRepository.AsSugarClient().Queryable<RouteInfo>()
                .OrderBy(it => it.FromStation, OrderByType.Asc)
                .OrderBy(it => it.ToStation, OrderByType.Asc)
                .ToListAsync();//GetListAsync();
            return list;
        }

        public async Task<bool> ModifyRouteInfoAsync(RouteInfo routeInfo)
        {
            bool isOk = await _RouteInfoRepository.UpdateAsync(routeInfo);

            return isOk;
        }

        public async Task<RouteInfo> GetRouteInfoByFromAndToAsync(string fromStation, string toStation)
        {
            try
            {
                var result = await _RouteInfoRepository.AsSugarClient().Queryable<RouteInfo>()
                    .Where(it => it.FromStation == fromStation && it.ToStation == toStation)
                    .FirstAsync();
                return result;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public async Task<List<RouteInfo>> GetRouteInfoListFromRedisAsync()
        {
            List<RouteInfo> list = new();
            list = _IRedisHelper.GetStringKey<List<RouteInfo>>("RouteInfo");
            if (list == null || list.Count == 0)
            {
                list = await _RouteInfoRepository.GetListAsync();
                _IRedisHelper.SetStringKey<List<RouteInfo>>("RouteInfo", list);
            }
            return list;
        }
    }
}
